home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Programmer Disk
/
The Programmer Disk (Microforum).iso
/
xpro
/
pascal3
/
pro11
/
convertb.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1987-06-10
|
4KB
|
148 lines
program convertb ;
{ Converts a Turbo data file created under CP/M to Dos format.
Reads the first four bytes of the file, two integers which
represent the number of records and the record length, computes
the length of the file in bytes, then reads that many bytes and
writes them to a new file.
Uses blockread to buffer reads of input file.
William Meacham, 1004 Elm Street, Austin, Tx 78703
Revised: 6/10/87 }
{$v-}
const
numreadsectors = 32 ; { number of sectors to read }
rblocksize = 128 ; { size of one block }
readbufsize = 4096 ; { rblocksize * numreadsectors }
type
intbytes = record case integer of
1: (i:integer) ;
2: (b1,b2:byte)
end ;
fname = string[56] ; { filename -- can include path, etc. }
rstr80 = string[80] ;
diskreadbuf = array [1..readbufsize] of byte ;
var
outf : file of byte ;
in_fname,
out_fname : fname ;
num_recs,
rec_len : intbytes ;
file_len,
num,
len,
r : real ;
b : byte ;
readbuf : diskreadbuf ; { buffer for reading file }
readptr : integer ; { pointer into buffer }
sourcefile : file ;
maxcharstoread : integer ; { limit to measure max buffer size }
allcharsread, { whether all chars in buffer have been read }
allblocksread : boolean ; { whether all blocks have been read into buffer }
{ ======================================== }
procedure bufread (var b:byte) ;
{ gets one byte }
procedure readnextblock ;
var
numsectorsread : integer ; { number of sectors actually read }
begin
blockread (sourcefile,readbuf,numreadsectors,numsectorsread) ;
if numsectorsread < numreadsectors then
begin
allblocksread := true ;
maxcharstoread := numsectorsread * rblocksize
end ;
if numsectorsread = 0 then
begin
allcharsread := true ;
maxcharstoread := 0
end ;
readptr := 1
end ; { proc readnextblock }
{ -------------------- }
begin { proc bufread }
if readptr > readbufsize then
readnextblock ;
if allblocksread and (readptr > maxcharstoread) then
allcharsread := true ;
if not allcharsread then
begin
b := readbuf[readptr] ;
readptr := succ(readptr) ;
end
else
b := $00
end ; { proc bufread }
{ ======================================== }
procedure opensourcefile (sfname : rstr80) ;
begin
assign (sourcefile,sfname) ;
reset (sourcefile) ;
allcharsread := false ;
allblocksread := false ;
readptr := readbufsize + 1 ; { forces read of first buffer }
end ; { proc opensourcefile }
{ ======================================== }
procedure closesourcefile ;
begin
close (sourcefile)
end ; { proc closesourcefile }
{ ======================================== }
begin
lowvideo ;
writeln ('This converts a CP/M-format Turbo data file') ;
writeln ('to a DOS-format data file.') ;
write (' Input file name: ') ;
readln (in_fname) ;
write ('Output file name: ') ;
readln (out_fname) ;
opensourcefile (in_fname) ;
assign (outf,out_fname) ;
rewrite (outf) ;
bufread (num_recs.b1) ;
bufread (num_recs.b2) ;
bufread (rec_len.b1) ;
bufread (rec_len.b2) ;
writeln ('Num_recs = ',num_recs.i) ;
writeln ('Rec_len = ',rec_len.i) ;
num := num_recs.i ; { convert to real }
len := rec_len.i ;
file_len := num * len ;
writeln ('File_len = ',file_len:8:0) ;
r := 1 ;
while r <= file_len do
begin
bufread (b) ;
write (outf,b) ;
r := r + 1.0
end ;
closesourcefile ;
close(outf) ;
writeln ('Done!',^G)
end.